/**
 * 练习数据 - 自主练习
 *
 * created by qiuwei
 * 2016年05月16日
 */

'use strict';

const builder = require('../filterBuilder');
const api = require('../dicAPIUrl');
const fillToMapHandler = require('../util').fillToMapHandler ;
const fillMultiDataToMapHandler = require('../util').fillMultiDataToMapHandler;

function request(requestData) {
	let filters = filter(requestData.condition);
	let timeUnit = requestData.currentTimeUnit;

	// 人数
	let peopleCount = {
		"data": {
			"target": "hurdle_record-student_id",
			"data_type": "count",
			"type": "normal"
		},
		"time_unit": timeUnit,
		"multi_unit": "",
		"filter": [].concat(filters)
	};

	// 次数
	let timeCount = {
		"data": {
			"target": "hurdle_record-hurdle_record_id",
			"data_type": "count",
			"type": "normal"
		},
		"time_unit": timeUnit,
		"multi_unit": "",
		"filter": [].concat(filters)
	};
	
	// 关卡数
	let missionCount = {
		"data": {
			"target": "hurdle_record-hurdle_id",
			"data_type": "count",
			"type": "normal"
		},

		"time_unit": timeUnit,
		"multi_unit": "hurdle_record-student_id",
		"filter": [].concat(filters)
	};

	return [peopleCount, timeCount, missionCount];
}

function response(peopleCount, timeCount, missionCount) {

	let set = new Set();
	let handler = fillToMapHandler.bind(set);

	let peopleCountMap = handler(peopleCount);
	let timeCountMap = handler(timeCount);
	let missionCountMap = fillMultiDataToMapHandler(missionCount);

	let rn = {
		categories: Array.from(set),
		peopleCountSeries: [],
		timeCountSeries: [],
		missionCountSeries: []
	};

	let peopleItem, timeItem, missionItem;
	rn.categories.forEach((item) => {
		rn.peopleCountSeries.push(+peopleCountMap[item] || 0);
		rn.timeCountSeries.push(+timeCountMap[item] || 0);
		rn.missionCountSeries.push(+missionCountMap[item] || 0);
	})

	return rn;
}

function filter(condition) {
	let filters = [];
	
    if (condition.province && condition.province != builder.NIL) {
		filters.push(builder.append('student-province_id', '=', condition.province));
	}
	if (condition.city && condition.city != builder.NIL) {
		filters.push(builder.append('student-city_id', '=', condition.city));
	}
	if (condition.county && condition.county != builder.NIL) {
		filters.push(builder.append('student-county_id', '=', condition.county));
	}
	if (condition.school && condition.school != builder.NIL) {
		filters.push(builder.append('student-school_id', '=', condition.school));
	}
	if (condition.stage[0] != builder.NIL) {
		filters.push(builder.append('student-grade_part', 'in', condition.stage.join(',')));
	}
	if (condition.grade[0] != builder.NIL) {
		filters.push(builder.append('student-grade', 'in', condition.grade.join(',')));
	}
	if (condition.subject[0] != builder.NIL) {
		filters.push(builder.append('hurdle_student_ext-hurdle_subject', 'in', condition.subject.join(',')));
	}
	if (condition.version != '0') {
		filters.push(builder.append('student-version', '=', condition.version));
	}
	if (condition.deviceType[0] != builder.NIL) {
		filters.push(builder.append('student-source', 'in', condition.deviceType.join(',')));
	}

	filters.push(builder.append('hurdle_record-day', '>=', condition.startDate));
	filters.push(builder.append('hurdle_record-day', '<=', condition.endDate));
	filters.push(builder.append('student-teacher_student_count', '>=', condition.userFeature));

	return filters;
}

module.exports = {
	API_URL: api.API_PACKAGE_DATA,
	request,
	response
};
